package com.particle.data.app.company.executor.warehouse;

import cn.hutool.core.util.StrUtil;
import com.particle.common.app.executor.AbstractBaseExecutor;
import com.particle.data.app.company.executor.DataCompanyAbnormalCreateCommandExecutor;
import com.particle.data.app.company.executor.DataCompanyAbnormalUpdateCommandExecutor;
import com.particle.data.app.company.executor.representation.exwarehouse.DataCompanyAbnormalExWarehouseCommandExecutor;
import com.particle.data.client.company.dto.command.DataCompanyAbnormalCreateCommand;
import com.particle.data.client.company.dto.command.DataCompanyAbnormalUpdateCommand;
import com.particle.data.client.company.dto.command.warehouse.DataCompanyAbnormalWarehouseCommand;
import com.particle.data.client.company.dto.data.DataCompanyAbnormalVO;
import com.particle.data.client.company.dto.data.exwarehouse.DataCompanyAbnormalExWarehouseVO;
import com.particle.data.infrastructure.company.service.IDataCompanyAbnormalService;
import com.particle.global.dto.response.SingleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

/**
 * <p>
 * 企业经营异常入库 指令执行器
 * </p>
 *
 * @author yw
 * @since 2025-05-29 11:05:41
 */
@Component
@Validated
public class DataCompanyAbnormalWarehouseCommandExecutor extends AbstractBaseExecutor {

	private DataCompanyAbnormalExWarehouseCommandExecutor dataCompanyAbnormalExWarehouseCommandExecutor;
	private DataCompanyAbnormalCreateCommandExecutor dataCompanyAbnormalCreateCommandExecutor;
	private IDataCompanyAbnormalService iDataCompanyAbnormalService;
	private DataCompanyAbnormalUpdateCommandExecutor dataCompanyAbnormalUpdateCommandExecutor;


	/**
	 * 企业经营异常入库
	 * @param dataCompanyAbnormalWarehouseCommand
	 * @return
	 */
	public SingleResponse<DataCompanyAbnormalExWarehouseVO> warehouse(DataCompanyAbnormalWarehouseCommand dataCompanyAbnormalWarehouseCommand) {
		SingleResponse<DataCompanyAbnormalExWarehouseVO> dataCompanyAbnormalExWarehouseVOSingleResponse = null;
		DataCompanyAbnormalExWarehouseVO dataCompanyAbnormalExWarehouseVO = null;
		String obtainDataMd5 = dataCompanyAbnormalWarehouseCommand.obtainDataMd5();
		if (StrUtil.isNotEmpty(obtainDataMd5)) {
			dataCompanyAbnormalExWarehouseVOSingleResponse = dataCompanyAbnormalExWarehouseCommandExecutor.exWarehouseByCompanyIdAndDataMd5(dataCompanyAbnormalWarehouseCommand.getCompanyId(),obtainDataMd5);
		}

		dataCompanyAbnormalExWarehouseVO = dataCompanyAbnormalExWarehouseVOSingleResponse == null ? null : dataCompanyAbnormalExWarehouseVOSingleResponse.getData();
		// 不存在，添加
        if (dataCompanyAbnormalExWarehouseVO == null) {
			DataCompanyAbnormalCreateCommand dataCompanyAbnormalCreateCommand = DataCompanyAbnormalCreateCommand.createByWarehouseCommand(dataCompanyAbnormalWarehouseCommand);
			SingleResponse<DataCompanyAbnormalVO> dataCompanyAbnormalVOSingleResponse = dataCompanyAbnormalCreateCommandExecutor.execute(dataCompanyAbnormalCreateCommand);
			Long id = dataCompanyAbnormalVOSingleResponse.getData().getId();
			// 新增后重新查询，返回最新数据
			dataCompanyAbnormalExWarehouseVOSingleResponse = dataCompanyAbnormalExWarehouseCommandExecutor.exWarehouseById(id);
			return dataCompanyAbnormalExWarehouseVOSingleResponse;
		}else {
			Long id = dataCompanyAbnormalExWarehouseVO.getId();
			// 	存在，尝试入库
			// 仅更新有变化的字段，将相同的字段设置为null不更新
			dataCompanyAbnormalWarehouseCommand.compareAndSetNullWhenEquals(dataCompanyAbnormalExWarehouseVO);

			// 判断是否所有字段都为空，所有字段都没有变化，不需要更新
			if (dataCompanyAbnormalWarehouseCommand.allFieldEmpty()) {
				// 更新最后处理时间
				iDataCompanyAbnormalService.updateLatestHandleAt(id);
				// 重新查询，返回最新数据
				dataCompanyAbnormalExWarehouseVOSingleResponse = dataCompanyAbnormalExWarehouseCommandExecutor.exWarehouseById(id);
				return dataCompanyAbnormalExWarehouseVOSingleResponse;
			} else {
				// 更新处理
				DataCompanyAbnormalUpdateCommand dataCompanyAbnormalUpdateCommand = DataCompanyAbnormalUpdateCommand.createByWarehouseCommand(
						id,
						dataCompanyAbnormalExWarehouseVO.getVersion(),
						dataCompanyAbnormalWarehouseCommand
				);
				dataCompanyAbnormalUpdateCommandExecutor.execute(dataCompanyAbnormalUpdateCommand);
				// 重新查询，返回最新数据
				dataCompanyAbnormalExWarehouseVOSingleResponse = dataCompanyAbnormalExWarehouseCommandExecutor.exWarehouseById(id);
				return dataCompanyAbnormalExWarehouseVOSingleResponse;
			}
		}
	}

	@Autowired
	public void setDataCompanyAbnormalExWarehouseCommandExecutor(DataCompanyAbnormalExWarehouseCommandExecutor dataCompanyAbnormalExWarehouseCommandExecutor) {
		this.dataCompanyAbnormalExWarehouseCommandExecutor = dataCompanyAbnormalExWarehouseCommandExecutor;
	}
	@Autowired
	public void setDataCompanyAbnormalCreateCommandExecutor(DataCompanyAbnormalCreateCommandExecutor dataCompanyAbnormalCreateCommandExecutor) {
		this.dataCompanyAbnormalCreateCommandExecutor = dataCompanyAbnormalCreateCommandExecutor;
	}
	@Autowired
	public void setiDataCompanyAbnormalService(IDataCompanyAbnormalService iDataCompanyAbnormalService) {
		this.iDataCompanyAbnormalService = iDataCompanyAbnormalService;
	}
	@Autowired
	public void setDataCompanyAbnormalUpdateCommandExecutor(DataCompanyAbnormalUpdateCommandExecutor dataCompanyAbnormalUpdateCommandExecutor) {
		this.dataCompanyAbnormalUpdateCommandExecutor = dataCompanyAbnormalUpdateCommandExecutor;
	}
}
